/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package east2west.action;

import east2west.dao.FlightDAO;
import east2west.dto.FlightsDTO;
import east2west.service.FlightsService;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author nguyendat
 */
public class FlightAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String NEW = "new";
    FlightsService flightSer = new FlightsService();

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward flightDetail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int id = Integer.parseInt(request.getParameter("id"));
        FlightDAO dao = new FlightDAO();
        FlightsDTO flightDTO = dao.findById(id);
        request.setAttribute("flightDTO", flightDTO);
        //findby orther id flight
        ArrayList<FlightsDTO> listOrther = dao.findAllOrtherId(id);
        request.setAttribute("listOrther", listOrther);

        request.setAttribute("page", "flightdetail.page");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward listFlight(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FlightsService serFlights = new FlightsService();
        ArrayList<FlightsDTO> list = serFlights.findByTopFlight();
        request.setAttribute("list", list);

        return mapping.findForward(NEW);

    }

    public ActionForward listFlightL(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FlightsService serFlights = new FlightsService();
        ArrayList<FlightsDTO> list = serFlights.readByAll();
        request.setAttribute("listflight", list);
        request.setAttribute("page", "flightlist.page");
        return mapping.findForward(SUCCESS);

    }

    public ActionForward searchFlight(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FlightsService serFlights = new FlightsService();
        HttpSession session = request.getSession();
        String departure = request.getParameter("departureS");
        String destination = request.getParameter("destinationS");
        FlightsDTO dto = new FlightsDTO();
        dto.setDeparture(departure);
        dto.setDestination(destination);
        ArrayList<FlightsDTO> listSearch = serFlights.findByDTO(dto);
        request.setAttribute("listSearch", listSearch);
        request.setAttribute("page", "flightlists.page");
        return mapping.findForward(SUCCESS);

    }
}
